package com.itcast.plane.util;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itcast.plane.modoule.company.domain.Company;
import com.itcast.plane.modoule.company.mapper.CompanyMapper;
import com.itcast.plane.modoule.flight.domain.Flight;
import com.itcast.plane.modoule.flight.mapper.FlightMapper;
import com.itcast.plane.modoule.indent.domain.Indent;
import com.itcast.plane.modoule.indent.mapper.IndentMapper;
import com.itcast.plane.modoule.path.domain.Path;
import com.itcast.plane.modoule.path.mapper.PathMapper;
import com.itcast.plane.modoule.person.domain.Person;
import com.itcast.plane.modoule.person.mapper.PersonMapper;
import com.itcast.plane.modoule.planes.domain.Planes;
import com.itcast.plane.modoule.planes.mapper.PlanesMapper;
import com.itcast.plane.modoule.ticket.domain.Ticket;
import com.itcast.plane.modoule.ticket.mapper.TicketMapper;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CommonService {

    @Resource
    private CompanyMapper companyMapper;

    @Resource
    private PathMapper pathMapper;

    @Resource
    private PlanesMapper planesMapper;

    @Resource
    private TicketMapper ticketMapper;

    @Resource
    private PersonMapper personMapper;

    @Resource
    private FlightMapper flightMapper;

    @Resource
    private IndentMapper indentMapper;

    public Company getCompanyById(int id) {
        return companyMapper.selectById(id);
    }

    public Path getPathById(int id) {
        return pathMapper.selectById(id);
    }

    public Planes getPlanesById(int id) {
        return planesMapper.selectById(id);
    }

    public Ticket getTicketById(int id){
        return ticketMapper.selectById(id);
    }

    public List<Path> getPathByLambda(LambdaQueryWrapper<Path> pathLambdaQueryWrapper) {
        return pathMapper.selectList(pathLambdaQueryWrapper);
    }

    public Person getPersonById(int id){
        return personMapper.selectById(id);
    }

    public void editTicket(Ticket ticket){
        ticketMapper.updateById(ticket);
    }

    public Flight getFlightById(int id){
        return flightMapper.selectById(id);
    }

    @Scheduled(cron = "0 0/10 * * * ?")
    public void DingShiSC(){
        System.out.println("定时任务运行了！");
        LambdaQueryWrapper<Flight> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        long now = TimeUtil.stopBuy();
        lambdaQueryWrapper.le(Flight::getDepartureTime,now).eq(Flight::getIfFly,0);
        List<Flight> listOne = flightMapper.selectList(lambdaQueryWrapper);
        for (Flight flight: listOne) {
            flight.setIfFly(1);
            flightMapper.updateById(flight);
            LambdaQueryWrapper<Indent> indentLambdaQueryWrapper = new LambdaQueryWrapper<>();
            indentLambdaQueryWrapper.eq(Indent::getFlightId,flight.getId());
            List<Indent> indentList = indentMapper.selectList(indentLambdaQueryWrapper);
            for (Indent indent: indentList) {
                indent.setIfFly(1);
                indentMapper.updateById(indent);
            }
        }
    }

    public List<Person> getPersonList(LambdaQueryWrapper<Person> lambdaQueryWrapper) {
        return personMapper.selectList(lambdaQueryWrapper);
    }
}
